<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人信息</title>
    <style>
        body {
            margin: 0;
            font-family: "Microsoft YaHei";
            background-color: #f4f6f9;
            color: #333;
        }

        /* 顶部导航栏 */
        .top-nav {
            width: 95%;
            height: 80px;
            background-color: #1E90FF;
            display: flex;
            align-items: center;
            justify-content: space-between;
            color: white;
            padding: 0 30px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .top-nav .logo {
            font-size: 25px;
            font-weight: bold;
            letter-spacing: 1px;
            margin-left: 20px;
        }

        .top-nav .user {
            font-size: 16px;
        }

        .container {
            display: flex;
            height: calc(100vh - 60px);
        }

        /* 左侧导航栏 */
        .sidebar {
            width: 200px;
            background-color: #2F4F9D;
            color: white;
            display: flex;
            flex-direction: column;
            padding-top: 30px;
        }

        .sidebar a {
            padding: 15px 20px;
            text-decoration: none;
            color: white;
            font-size: 16px;
            display: block;
            text-align: center;
        }

        .sidebar a:hover,
        .sidebar a.active {
            background-color: #4169E1;
            border-left: 5px solid #FFD700;
        }

        /* 主体内容 */
        .main-content {
            flex: 1;
            padding: 20px 40px;
        }

        .main-content h1 {
            color: #1E90FF;
            font-size: 26px;
            margin-bottom: 20px;
        }

        .profile-card {
            background: white;
            border-radius: 10px;
            padding: 40px;
            max-width: 800px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        /* 信息表格样式 */
        .info-table {
            width: 100%;
            border-collapse: separate;
            border-spacing: 0 20px;
        }

        .info-table td {
            font-size: 16px;
            vertical-align: middle;
        }

        .info-table td:first-child {
            width: 140px;
            color: #555;
            font-weight: 500;
            text-align: right;
            padding-right: 30px;
        }

        .info-table td:last-child {
            color: #333;
        }

        /* 密码相关样式 */
        .password-container {
            display: flex;
            align-items: center;
        }

        .password-container span {
            margin-right: 10px;
        }

        .toggle-pwd-btn {
            background: none;
            border: none;
            color: #1E90FF;
            cursor: pointer;
            padding: 2px 5px;
            font-size: 14px;
        }

        .toggle-pwd-btn:hover {
            text-decoration: underline;
        }

        /* 余额卡片样式 */
        .balance-section {
            margin-top: 40px;
            padding-top: 30px;
            border-top: 1px solid #eee;
        }

        .balance-section h2 {
            color: #1E90FF;
            font-size: 20px;
            margin-bottom: 25px;
        }

        .balance-cards {
            display: flex;
            gap: 30px;
            flex-wrap: wrap;
        }

        .balance-card {
            flex: 1;
            min-width: 250px;
            background: linear-gradient(135deg, #e8f4f8 0%, #f0f8fb 100%);
            border-radius: 8px;
            padding: 25px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
        }

        .balance-card h3 {
            color: #4a5568;
            font-size: 16px;
            margin-bottom: 15px;
            font-weight: 500;
        }

        .balance-amount {
            font-size: 28px;
            color: #1E90FF;
            font-weight: bold;
            margin-bottom: 8px;
        }

        .balance-desc {
            font-size: 14px;
            color: #718096;
        }

        .btn {
            background-color: #FFD700;
            color: #333;
            border: none;
            border-radius: 5px;
            padding: 8px 16px;
            cursor: pointer;
            font-size: 14px;
            transition: background-color 0.3s;
        }

        .btn:hover {
            background-color: #e6c200;
        }

        .btn-primary {
            background-color: #1E90FF;
            color: white;
        }

        .btn-primary:hover {
            background-color: #187bcd;
        }

        #password-input {
            display: none;
            margin-top: 15px;
            padding: 15px;
            background-color: #f9f9f9;
            border-radius: 5px;
        }

        #password-input input {
            padding: 8px 10px;
            width: 250px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-right: 10px;
            margin-bottom: 10px;
        }
    </style>
</head>

<body>

<!-- 顶部导航栏 -->
<div class="top-nav">
    <div class="logo">智慧银行系统</div>
    <div class="user" id="user-info">加载中...</div>
</div>

<div class="container">
    <!-- 左侧导航栏 -->
    <div class="sidebar">
        <a href="index.html">首页</a>
        <a href="deposit.html">存款服务</a>
        <a href="withdraw.html">取款服务</a>
        <a href="transfer.html">转账服务</a>
        <a href="transactions.html">账户流水</a>
        <a href="user.html" class="active">个人中心</a>
    </div>

    <!-- 主体内容 -->
    <div class="main-content">
        <h1>个人中心</h1>
        <div class="profile-card">
            <table class="info-table">
                <tr>
                    <td>姓名：</td>
                   <td id="name"></td>
                </tr>
                <tr>
                    <td>性别：</td>
                    <td id="gender"></td>
                </tr>
                <tr>
                    <td>身份证号：</td>
                    <td id="id"></td>
                </tr>
                <tr>
                    <td>绑定手机号：</td>
                    <td id="phone"></td>
                </tr>
                <tr>
                    <td>银行卡号：</td>
                    <td id="bank-card"></td>
                </tr>
                <tr>
                    <td>登录密码：</td>
                    <td>
                        <div class="password-container">
                            <span id="password">********</span>
                            <button class="toggle-pwd-btn" onclick="togglePasswordVisibility()">
                                显示
                            </button>
                            <button class="btn" onclick="togglePasswordChange()">修改密码</button>
                        </div>

                        <div id="password-input">
                            <input type="password" id="old-password" placeholder="请输入原密码">
                            <input type="password" id="new-password" placeholder="请输入新密码">
                            <input type="password" id="confirm-password" placeholder="请确认新密码">
                            <button class="btn btn-primary" onclick="savePassword()">确认修改</button>
                        </div>
                    </td>
                </tr>
            </table>

            <div class="balance-section">
                <h2>账户余额</h2>
                <div class="balance-cards">
                    <div class="balance-card">
                        <h3>活期存款</h3>
                        <div class="balance-amount" id="current-balance"></div>
                        <div class="balance-desc">实时可用，随存随取</div>
                    </div>
                    <div class="balance-card">
                        <h3>定期存款</h3>
                        <div class="balance-amount" id="fixed-balance"></div>
<!--                        <div class="balance-desc" id="fixed-desc"></div>-->
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    // 加载用户 + 账户信息
    window.onload = async function () {

        const user = JSON.parse(localStorage.getItem("user"));
        if (!user) {
            window.location.href = "login.html";
            return;
        }

        // 顶部欢迎语
        const title = user.gender === "男" ? "先生" : "女士";
        document.getElementById("user-info").textContent = `亲爱的 ${user.name}${title}`;
        console.log("user.userId:", user.userId, typeof user.userId);

        // 请求后端
        const res = await fetch(`/user/${user.userId}/profile`);
        const data = await res.json();

        const u = data.user;
        const a = data.account;

        document.getElementById("name").textContent = u.name||u.username;
        document.getElementById("gender").textContent = u.gender;
        document.getElementById("id").textContent = u.id_card;
        document.getElementById("phone").textContent = u.phone;

        document.getElementById("bank-card").textContent = a.cardNumber;
        document.getElementById("current-balance").textContent = "¥" + a.balance;
        document.getElementById("fixed-balance").textContent = "¥" + a.fixedBalance;

        currentPassword = u.password;

        // 定期存款详情
        // document.getElementById("fixed-desc").textContent =
        //     `到期利率：${a.fixedRate}% ，到期日：${a.fixedDate}`;

    };
    // 密码显示状态
    let pwdVisible = false;

    // 用于显示的密码
    let currentPassword = "";

    // ================================
    // 显示 / 隐藏密码
    // ================================
    function togglePasswordVisibility() {
        const pwdSpan = document.getElementById("password");
        const btn = event.target;

        if (pwdVisible) {
            pwdSpan.textContent = "********";
            btn.textContent = "显示";
        } else {
            pwdSpan.textContent = currentPassword; // 真实密码
            btn.textContent = "隐藏";
        }

        pwdVisible = !pwdVisible;
    }

    // ================================
    // 展开 / 收起 修改密码框
    // ================================
    function togglePasswordChange() {
        const box = document.getElementById("password-input");
        box.style.display = box.style.display === "block" ? "none" : "block";
    }

    // ================================
    // 保存密码（你可替换为后端 API）
    // ================================
    function savePassword() {
        const oldPwd = document.getElementById("old-password").value;
        const newPwd = document.getElementById("new-password").value;
        const confirmPwd = document.getElementById("confirm-password").value;

        if (oldPwd !== currentPassword) {
            alert("原密码错误！");
            return;
        }

        if (newPwd.length < 6) {
            alert("密码不能少于 6 位！");
            return;
        }

        if (newPwd !== confirmPwd) {
            alert("两次输入的新密码不一致！");
            return;
        }

        // 纯前端更新（你可改成 fetch 后端接口）
        currentPassword = newPwd;

        if (pwdVisible) {
            document.getElementById("password").textContent = newPwd;
        }

        alert("密码修改成功！");
        document.getElementById("password-input").style.display = "none";
    }

</script>

</body>

</html>
